﻿namespace BlackHole
{
    using System;
    using System.Web.UI.WebControls;
    using BlackHole.DAO;

    public partial class Maintance : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RenderUI();
            }
        }

        protected void RenderUI()
        {
            RefreshDeviceList();
            RefreshDistrictList();
            RefreshOwnerList(0);
        }

        protected void lstDistricts_SelectedIndexChanged(object sender, EventArgs e)
        {
            int ownerId;
            ownerId = int.Parse(lstDistricts.SelectedValue);
            if (ownerId != 0) RefreshOwnerList(ownerId);
            ShowAddButton(2);
        }

        protected void RefreshDeviceList()
        {
            txtAddName.Text = string.Empty;

            lstDevices.Items.Clear();
            lstDevices.Items.Add(new ListItem("(此处新增)", "0"));
            foreach (Device d in Utility.GetAllDevices())
            {
                lstDevices.Items.Add(new ListItem(d.DeviceName, d.Id.ToString()));
            }
        }

        protected void RefreshDistrictList()
        {
            lstDistricts.Items.Clear();
            lstDistricts.Items.Add(new ListItem("(此处新增)", "0"));
            foreach (District d in Utility.GetAllDistricts())
            {
                lstDistricts.Items.Add(new ListItem(d.DistrictName, d.Id.ToString()));
            }
        }

        protected void RefreshOwnerList(int ownerId)
        {
            lstOwners.Items.Clear();
            foreach (Owner o in Utility.GetOwnersByDistrictId(ownerId))
            {
                lstOwners.Items.Add(new ListItem(o.OwnerName, o.Id.ToString()));
            }
        }

        protected void lstDevices_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowAddButton(1);
        }

        protected void lstOwners_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowAddButton(3);
        }

        protected void ShowAddButton(int type)
        {
            btnAddDevice.Visible = false;
            btnAddDistrict.Visible = false;
            btnAddOwner.Visible = false;

            switch (type)
            {
                case 1:
                    lblTips.Text = "新增设备类型";
                    btnAddDevice.Visible = true;
                    break;
                case 2:
                    lblTips.Text = "新增区县";
                    btnAddDistrict.Visible = true;
                    break;
                case 3:
                    lblTips.Text = "新增网点";
                    btnAddOwner.Visible = true;
                    break;
                default:
                    break;
            }
        }

        protected void btnAddDevice_Click(object sender, EventArgs e)
        {
            if (txtAddName.Text.Length > 0)
            {
                lblTips.Text =
                    Utility.DeviceAddNew(txtAddName.Text) ? "设备新增成功" : "设备新增失败";
            }

            RefreshDeviceList();
        }

        protected void btnAddDistrict_Click(object sender, EventArgs e)
        {
            if (txtAddName.Text.Length > 0)
            {
                lblTips.Text =
                    Utility.DistrictAddNew(txtAddName.Text) ? "区县新增成功" : "区县新增失败";
            }

            RefreshDistrictList();
        }

        protected void btnAddOwner_Click(object sender, EventArgs e)
        {
            if (txtAddName.Text.Length > 0)
            {
                lblTips.Text =
                    Utility.OwnerAddNew(int.Parse(lstDistricts.SelectedValue), txtAddName.Text) ? "网点新增成功" : "网点新增失败";
            }

            RefreshOwnerList(0);
        }

        protected void dataProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtUpdateSerialNO.Text = dataProducts.SelectedDataKey["SerialNO"].ToString();
            txtUpdatePsamNO.Text = dataProducts.SelectedDataKey["PsamNO"].ToString();
            txtUpdateRomVersion.Text = dataProducts.SelectedDataKey["RomVersion"].ToString();
            comboNewDistricts.SelectedValue = dataProducts.SelectedDataKey["ProductDistrictId"].ToString();
            comboNewOwners.DataBind();
            comboNewOwners.SelectedValue = dataProducts.SelectedDataKey["ProductOwnerId"].ToString();
            comboNewDevices.SelectedValue = dataProducts.SelectedDataKey["ProductDeviceId"].ToString();
        }

        protected void dataProducts_PageIndexChanged(object sender, EventArgs e)
        {
        }

        protected void dataProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
        }

        protected void dataProducts_DataBinding(object sender, EventArgs e)
        {
        }

        protected void btnUpdateProduct_Click(object sender, EventArgs e)
        {
            bool ret = Utility.ProductUpdate(
                int.Parse(dataProducts.SelectedDataKey["Id"].ToString()),
                int.Parse(comboNewOwners.SelectedValue),
                int.Parse(comboNewDevices.SelectedValue),
                txtUpdateSerialNO.Text,
                txtUpdatePsamNO.Text,
                txtUpdateRomVersion.Text);
            lblErr.Text =
                ret == true ? "更新成功" : "更新失败";
        }
    }
}